<!DOCTYPE html>


<html lang="en">
  

    <head>
      <meta charset="utf-8" />
        
      <meta
        name="viewport"
        content="width=device-width, initial-scale=1, maximum-scale=1"
      />
      <title> 朝花夕拾</title>
  <meta name="generator" content="hexo-theme-ayer">
      
      <link rel="shortcut icon" href="/ww.ico" />
       
<link rel="stylesheet" href="/dist/main.css">

      
<link rel="stylesheet" href="/css/fonts/remixicon.css">

      
<link rel="stylesheet" href="/css/custom.css">
 
      <script src="https://cdn.staticfile.org/pace/1.2.4/pace.min.js"></script>
       
 

      <link
        rel="stylesheet"
        href="https://cdn.jsdelivr.net/npm/@sweetalert2/theme-bulma@5.0.1/bulma.min.css"
      />
      <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11.0.19/dist/sweetalert2.min.js"></script>

      <!-- mermaid -->
      
      <style>
        .swal2-styled.swal2-confirm {
          font-size: 1.6rem;
        }
      </style>
    </head>
  </html>
</html>


<body>
  <div id="app">
    
      
    <main class="content on">
      
<section class="cover">
    
  <div class="cover-frame">
    <div class="bg-box">
      <img src="/images/cover8.jpg" alt="image frame" />
    </div>
    <div class="cover-inner text-center text-white">
      <h1><a href="/">朝花夕拾</a></h1>
      <div id="subtitle-box">
        
        <span id="subtitle"></span>
        
      </div>
      <div>
        
      </div>
    </div>
  </div>
  <div class="cover-learn-more">
    <a href="javascript:void(0)" class="anchor"><i class="ri-arrow-down-line"></i></a>
  </div>
</section>



<script src="https://cdn.staticfile.org/typed.js/2.0.12/typed.min.js"></script>


<!-- Subtitle -->

  <script>
    try {
      var typed = new Typed("#subtitle", {
        strings: ['', '', ''],
        startDelay: 0,
        typeSpeed: 200,
        loop: true,
        backSpeed: 100,
        showCursor: true
      });
    } catch (err) {
      console.log(err)
    }
  </script>
  
<div id="main">
  <section class="outer">
  
  
  

<div class="notice" style="margin-top:50px">
    <i class="ri-heart-fill"></i>
    <div class="notice-content" id="broad"></div>
</div>
<script type="text/javascript">
    fetch('https://v1.hitokoto.cn')
        .then(response => response.json())
        .then(data => {
            document.getElementById("broad").innerHTML = data.hitokoto;
        })
        .catch(console.error)
</script>

<style>
    .notice {
        padding: 20px;
        border: 1px dashed #e6e6e6;
        color: #969696;
        position: relative;
        display: inline-block;
        width: 100%;
        background: #fbfbfb50;
        border-radius: 10px;
    }

    .notice i {
        float: left;
        color: #999;
        font-size: 16px;
        padding-right: 10px;
        vertical-align: middle;
        margin-top: -2px;
    }

    .notice-content {
        display: initial;
        vertical-align: middle;
    }
</style>
  
  <article class="articles">
    
    
    
    
    <article
  id="post-Keepalive高可用GBase8s"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h2 itemprop="name">
  <a class="article-title" href="/2023/04/23/Keepalive%E9%AB%98%E5%8F%AF%E7%94%A8GBase8s/"
    >Keepalive高可用GBase8s</a> 
</h2>
 

      
    </header>
     
    <div class="article-meta">
      <a href="/2023/04/23/Keepalive%E9%AB%98%E5%8F%AF%E7%94%A8GBase8s/" class="article-date">
  <time datetime="2023-04-23T02:24:30.000Z" itemprop="datePublished">2023-04-23</time>
</a> 
  <div class="article-category">
    <a class="article-category-link" href="/categories/Develop/">Develop</a>
  </div>
   
    </div>
      
    <div class="article-entry" itemprop="articleBody">
       
  <h2 id="软件附件存储位置："><a href="#软件附件存储位置：" class="headerlink" title="软件附件存储位置："></a>软件附件存储位置：</h2><p>   本地存储位置： D:\soft\09.博客附件\KeepalivedGBase8s<br>   网络存储位置： <a target="_blank" rel="noopener" href="https://pan.baidu.com/s/1UQCZpYVxYF7pBHsTUFjmzA">点击下载</a>  提取码：6789</p>
<h2 id="部署方法"><a href="#部署方法" class="headerlink" title="部署方法"></a>部署方法</h2><p>1,  编辑 config_files.txt 填写你的环境的真实数据</p>
<p>2,  执行  .&#x2F;config_files.sh</p>
<p>3,  把 config_files 目录的文件上传到正确的为位置即可。</p>
<p>4，先启动数据库2个节点的集群，再启动主节点的keeplive进程，再启动备节点的keeplive进程。</p>
<h2 id="软件说明"><a href="#软件说明" class="headerlink" title="软件说明"></a>软件说明</h2><ul>
<li><p>功能</p>
<ol>
<li><p>提供vip</p>
</li>
<li><p>提供故障停止库和切换</p>
</li>
<li><p>提供启动时检查是否有主，有加入集群，没有则自启动为主。</p>
</li>
</ol>
</li>
<li><p>组件</p>
<ol>
<li>每2秒执行&#x2F;opt&#x2F;gbkeepalived&#x2F;gbase8s_check.sh检查GBase数据库是否可用，如果不可用，停止keeplive进程。</li>
<li>keeplive升级为主时执行&#x2F;opt&#x2F;gbkeepalived&#x2F;master.sh停止复制关系并升级为主数据库。</li>
<li>keeplive停止时执行&#x2F;opt&#x2F;gbkeepalived&#x2F;stop.sh停止数据库。</li>
<li>oninit.sh为启动脚本，启动数据库时检查集群是否有主存活，有加入集群，没有则自启动为主。并启动keeplive进程。</li>
</ol>
</li>
</ul>
<h2 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h2><pre><code>    调研GBase8s keepalive高可用解决方案，要达到两个主要目标：主库出现问题时快速自动切换到从库；对应用透明。
</code></pre>
<h2 id="原理"><a href="#原理" class="headerlink" title="原理"></a>原理</h2><p>VRRP 虚拟路由冗余协议</p>
<p>Virtual Router Redundancy Protocol</p>
<p>构建多个路由器,将几台路由器组成一个集群.</p>
<p>多个backup路由器由权重选举新的master.</p>
<p>master节点会不停的发送心跳给backup节点,当backup节点接收不到心跳,它会认为master故障了,backup选举.</p>
<h2 id="下载安装"><a href="#下载安装" class="headerlink" title="下载安装"></a>下载安装</h2><p>官网 <a target="_blank" rel="noopener" href="https://www.keepalived.org/index.html">https://www.keepalived.org/index.html</a></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">[root@h11 opt]# tar zxvf keepalived-2.2.7.tar.gz</span><br><span class="line">[root@h12 opt]# tar zxvf keepalived-2.2.7.tar.gz</span><br><span class="line"></span><br><span class="line">[root@h11 keepalived-2.2.7]# yum -y install gcc</span><br><span class="line">[root@h11 keepalived-2.2.7]# yum -y install openssl</span><br><span class="line">[root@h11 keepalived-2.2.7]# yum -y install openssl-devel</span><br><span class="line">[root@h11 opt]# cd keepalived-2.2.7</span><br><span class="line">[root@h11 keepalived-2.2.7]# ./configure --prefix=/usr/local/keepalived --sysconf=/etc</span><br><span class="line">[root@h11 keepalived-2.2.7]# make &amp;&amp; make install</span><br><span class="line">[root@h11 keepalived-2.2.7]# whereis keepalived</span><br></pre></td></tr></table></figure>



<h2 id="把keepalived注册为系统服务"><a href="#把keepalived注册为系统服务" class="headerlink" title="把keepalived注册为系统服务"></a>把keepalived注册为系统服务</h2><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">cd /opt/keepalived-2.2.7/keepalived/etc</span><br><span class="line">cp init.d/keepalived /etc/init.d/</span><br><span class="line">cp sysconfig/keepalived /etc/sysconfig/</span><br><span class="line">systemctl daemon-reload</span><br><span class="line">systemctl restart keepalived</span><br><span class="line">systemctl enable keepalived</span><br><span class="line">systemctl status keepalived</span><br></pre></td></tr></table></figure>



<h2 id="GBase8s-Keepalived-高可用部署"><a href="#GBase8s-Keepalived-高可用部署" class="headerlink" title="GBase8s+Keepalived 高可用部署"></a>GBase8s+Keepalived 高可用部署</h2><h3 id="1-环境"><a href="#1-环境" class="headerlink" title="1. 环境"></a>1. 环境</h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">环境：</span></span><br><span class="line">         GBase8s Primary        192.168.1.11</span><br><span class="line">         GBase8s Secondary      192.168.1.12</span><br><span class="line">         VIP              		192.168.1.13/24</span><br><span class="line">         GBase8s主从HDR复制配置从略。</span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">DR:</span></span><br><span class="line">DRAUTO                  0</span><br><span class="line">DRINTERVAL              0</span><br><span class="line">HDR_TXN_SCOPE           NEAR_SYNC</span><br><span class="line">DRTIMEOUT               3</span><br><span class="line">onmode -wf DRTIMEOUT=3</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">SQLHOSTS:</span></span><br><span class="line">g330pri onsoctcp 192.168.1.11 9083</span><br><span class="line">g330hdr onsoctcp 192.168.1.12 9083</span><br><span class="line">g330vip onsoctcp 0.0.0.0      9088</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">192.168.1.11的ONCONFIG:</span></span><br><span class="line">DBSERVERNAME g330pri</span><br><span class="line">DBSERVERALIASES g330vip</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">192.168.1.12的ONCONFIG:</span></span><br><span class="line">DBSERVERNAME g330hdr</span><br><span class="line">DBSERVERALIASES g330vip</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">sqlhosts.vip (客户端连接用)</span></span><br><span class="line">g330vip onsoctcp 192.138.1.13  9088</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>环境变量</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">if [ -f ~/.bashrc ]; then</span><br><span class="line">        . ~/.bashrc</span><br><span class="line">fi</span><br><span class="line">PATH=$PATH:$HOME/.local/bin:$HOME/bin</span><br><span class="line">export PATH</span><br><span class="line"></span><br><span class="line">export GBASEDBTDIR=/opt/install/g330</span><br><span class="line">export GBASEDBTSERVER=$server__node</span><br><span class="line">export ONCONFIG=onconfig.$&#123;GBASEDBTSERVER&#125;</span><br><span class="line">export PATH=$&#123;GBASEDBTDIR&#125;/bin:$&#123;PATH&#125;:.</span><br><span class="line">export GBASEDBTSQLHOSTS=$&#123;GBASEDBTDIR&#125;/etc/sqlhosts</span><br><span class="line">export LD_LIBRARY_PATH=$GBASEDBTDIR/lib:$GBASEDBTDIR/lib/esql:$GBASEDBTDIR/lib/cli:$GBASEDBTDIR/lib/dmi</span><br><span class="line">export DB_LOCALE=zh_CN.utf8</span><br><span class="line">export CLIENT_LOCALE=zh_CN.utf8</span><br><span class="line">export GL_USEGLU=1</span><br><span class="line">export DBDATE=&quot;Y4MD-&quot;</span><br><span class="line">export DBACCESS_SHOW_TIME=1</span><br><span class="line"></span><br><span class="line">export GBASEDBTCONTIME=1</span><br><span class="line">export GBASEDBTCONRETRY=0</span><br><span class="line">export GBASEDBTOFFSETS=1</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>ONCONFIG</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"></span><br></pre></td></tr></table></figure>





<h3 id="2-高可用配置"><a href="#2-高可用配置" class="headerlink" title="2. 高可用配置"></a>2. 高可用配置</h3><ul>
<li><p>keepalived配置文件如下：&#x2F;etc&#x2F;keepalived&#x2F;keepalived.conf</p>
<p><strong>直接复制下面框体内容,创建&#x2F;etc&#x2F;keepalived&#x2F;keepalived.conf文件,主节点100从节点90即可</strong></p>
</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line">global_defs &#123;</span><br><span class="line">   router_id GBASE8S-HA</span><br><span class="line">&#125; </span><br><span class="line"></span><br><span class="line">vrrp_script check_run &#123;</span><br><span class="line">script &quot;/home/gbasedbt/gbase8s_check.sh&quot;</span><br><span class="line">interval 2</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">vrrp_sync_group VG1 &#123;</span><br><span class="line">group &#123;</span><br><span class="line">VI_1</span><br><span class="line">&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">vrrp_instance VI_1 &#123;</span><br><span class="line">    state BACKUP</span><br><span class="line">    interface eth0 </span><br><span class="line">    virtual_router_id 51</span><br><span class="line">    #priority 100</span><br><span class="line">    priority 90  </span><br><span class="line">    advert_int 1</span><br><span class="line">    nopreempt</span><br><span class="line">    authentication &#123;</span><br><span class="line">        auth_type PASS</span><br><span class="line">        auth_pass 1234</span><br><span class="line">    &#125;</span><br><span class="line">    track_script &#123;</span><br><span class="line">    check_run</span><br><span class="line">    &#125;</span><br><span class="line">    notify_master /home/gbasedbt/master.sh</span><br><span class="line">    notify_stop /home/gbasedbt/stop.sh</span><br><span class="line"></span><br><span class="line">    virtual_ipaddress &#123;</span><br><span class="line">        192.168.1.13/24</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<pre><code>    master与slave的keepalived配置文件中只有priority设置不同，master为100，slave为90，其它全一样。nopreempt参数是priority大的后启动,不会去抢占master.
</code></pre>
<ul>
<li><p>&#x2F;home&#x2F;gbasedbt&#x2F;gbase8s_check.sh文件用以检测GBase8s服务是否正常，当发现连接不上gbase8s，自动把keepalived进程杀掉，让VIP进行漂移。文件内容如下。</p>
<p><strong>直接复制下面框体内容,创建&#x2F;home&#x2F;gbasedbt&#x2F;gbase8s_check.sh文件,并給755权限即可</strong></p>
</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">!/bin/bash</span></span><br><span class="line">. /home/gbasedbt/.bash_profile</span><br><span class="line">export GBASEDBTCONTIME=1</span><br><span class="line">export GBASEDBTCONRETRY=0</span><br><span class="line">export GBASEDBTOFFSETS=1</span><br><span class="line">count=1</span><br><span class="line"></span><br><span class="line">while true</span><br><span class="line">do</span><br><span class="line"></span><br><span class="line">i=$(echo &#x27;select 123456654321 from sysdual;&#x27; |dbaccess sysmaster 2&gt;/dev/null |grep 123456654321 |wc -l)</span><br><span class="line">j=$(onstat - |grep &#x27;not initialized&#x27; |wc -l)</span><br><span class="line"></span><br><span class="line">if [ $i = 1 ] &amp;&amp; [ $j = 0 ]</span><br><span class="line">then</span><br><span class="line">   exit 0</span><br><span class="line">else</span><br><span class="line">   if [ $i = 0 ] &amp;&amp; [ $j = 0 ]</span><br><span class="line">   then </span><br><span class="line">   	  exit 0</span><br><span class="line">   else</span><br><span class="line">   		if [ $count -gt 5 ]</span><br><span class="line">   			then</span><br><span class="line">      			break</span><br><span class="line">   		fi</span><br><span class="line">   		let count++</span><br><span class="line">   		continue</span><br><span class="line">   fi</span><br><span class="line">fi</span><br><span class="line"></span><br><span class="line">done</span><br><span class="line"></span><br><span class="line">/etc/init.d/keepalived stop</span><br><span class="line"></span><br></pre></td></tr></table></figure>





<ul>
<li><p>&#x2F;home&#x2F;gbasedbt&#x2F;master.sh的作用是状态改为master以后执行的脚本。首先判断复制是否有延迟，如果有日志延迟，等1分钟后，不论是否有延迟，都停止复制， 并自立为主。文件内容如下。</p>
<p><strong>直接复制下面框体内容,创建&#x2F;home&#x2F;gbasedbt&#x2F;master.sh文件,并給755权限即可</strong></p>
</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">!/bin/bash</span></span><br><span class="line"></span><br><span class="line">. /home/gbasedbt/.bash_profile</span><br><span class="line"></span><br><span class="line">i=1</span><br><span class="line"></span><br><span class="line">while true</span><br><span class="line">do</span><br><span class="line"></span><br><span class="line">ID1=$(onstat -l |grep C|awk &#x27;&#123;print $4&#125;&#x27;)</span><br><span class="line">POS1=$(onstat -l |grep C|awk &#x27;&#123;print $7&#125;&#x27;)</span><br><span class="line">sleep 3</span><br><span class="line">ID2=$(onstat -l |grep C|awk &#x27;&#123;print $4&#125;&#x27;)</span><br><span class="line">POS2=$(onstat -l |grep C|awk &#x27;&#123;print $7&#125;&#x27;)</span><br><span class="line"></span><br><span class="line">if [ $ID1 = $ID2 ] &amp;&amp; [ $POS1 = $POS2 ]</span><br><span class="line">then</span><br><span class="line">   echo &quot;ok&quot;</span><br><span class="line">   break</span><br><span class="line">else</span><br><span class="line">   if [ $i -gt 20 ]</span><br><span class="line">   then</span><br><span class="line">      break</span><br><span class="line">   fi</span><br><span class="line">   let i++</span><br><span class="line">   continue</span><br><span class="line">fi</span><br><span class="line">done</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">停止复制关系</span></span><br><span class="line">while true</span><br><span class="line">do</span><br><span class="line">	sleep 1</span><br><span class="line">	su - gbasedbt -c &quot;onmode -d standard&quot;</span><br><span class="line"><span class="meta prompt_">	#</span><span class="language-bash">su - gbasedbt -c <span class="string">&quot;onmode -d make primary <span class="variable">$GBASEDBTSERVER</span> force&quot;</span></span></span><br><span class="line">	if  [ $? = 0 ]</span><br><span class="line">	then</span><br><span class="line">		break</span><br><span class="line">	fi</span><br><span class="line">done</span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">切换新主</span></span><br><span class="line">objectServer=$(cat $GBASEDBTDIR/etc/sqlhosts |grep -vE &quot;$GBASEDBTSERVER|g330vip&quot;|awk &#x27;&#123;print $1&#125;&#x27;)</span><br><span class="line">su - gbasedbt -c &quot;onmode -d primary $objectServer&quot;</span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">记录当前状态</span></span><br><span class="line">echo &quot;$(date &#x27;+%y%m%d-%H%M&#x27;) $GBASEDBTSERVER up to Primary.&quot; &gt;&gt; /tmp/online.keeplive.log</span><br><span class="line"></span><br></pre></td></tr></table></figure>



<ul>
<li>&#x2F;home&#x2F;gbasedbt&#x2F;stop.sh 当keepalived停止的时候把数据库也停下来</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">!/bin/bash</span></span><br><span class="line"></span><br><span class="line">. /home/gbasedbt/.bash_profile</span><br><span class="line"></span><br><span class="line">echo &quot;$(date &#x27;+%y%m%d-%H%M&#x27;) Keepalived Stoped.&quot; &gt;&gt; /tmp/online.keeplive.log</span><br><span class="line">su - gbasedbt -c &quot;onclean -ky&quot;</span><br><span class="line">systemctl stop gbasedbtd</span><br><span class="line">echo &quot;$(date &#x27;+%y%m%d-%H%M&#x27;) GBase8s Stoped.&quot; &gt;&gt; /tmp/online.keeplive.log</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>






<ul>
<li><p>crt_gbasedbtd.sh 把gbase8s配置成服务,开机自启动数据库,并再次启动Keepalived服务.关机时自关闭数据库.</p>
<p>说明:启动数据库的时候,如果自己原来是主机,但是现在对端已经抢占了主身份,启动会失败,这时候用PHY启动并加入集群.</p>
<p><strong>直接复制下面框体内容,然后root用户粘贴即可.</strong></p>
</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">gbasedbtd服务环境基础</span></span><br><span class="line">. /home/gbasedbt/.bash_profile</span><br><span class="line">mkdir -p /etc/sysconfig</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">gbasedbtd服务环境</span></span><br><span class="line">if [ $GBASEDBTSQLHOSTS ] ; then</span><br><span class="line">	SQLHOSTS=$GBASEDBTSQLHOSTS</span><br><span class="line">else</span><br><span class="line">	SQLHOSTS=$GBASEDBTDIR/etc/sqlhosts</span><br><span class="line">fi</span><br><span class="line">cat &gt; /etc/sysconfig/gbasedbtd_service &lt;&lt; EOF</span><br><span class="line">GBASEDBTDIR=$GBASEDBTDIR</span><br><span class="line">GBASEDBTSERVER=$GBASEDBTSERVER</span><br><span class="line">ONCONFIG=$ONCONFIG</span><br><span class="line">GBASEDBTSQLHOSTS=$SQLHOSTS</span><br><span class="line">EOF</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">gbasedbtd服务</span></span><br><span class="line">cat &gt; /usr/lib/systemd/system/gbasedbtd.service &lt;&lt; EOF</span><br><span class="line">[Unit]</span><br><span class="line">Description=GBase 8s Database Server</span><br><span class="line">Documentation=file:$GBASEDBTDIR/release/en_us/0333</span><br><span class="line">Wants=network-online.target</span><br><span class="line">After=network.target network-online.target</span><br><span class="line"></span><br><span class="line">[Service]</span><br><span class="line">Type=oneshot</span><br><span class="line">User=root</span><br><span class="line">Group=root</span><br><span class="line">RemainAfterExit=yes</span><br><span class="line">EnvironmentFile=/etc/sysconfig/gbasedbtd_service</span><br><span class="line">ExecStart=$GBASEDBTDIR/bin/oninit.sh</span><br><span class="line">ExecStop=$GBASEDBTDIR/bin/onmode -ky</span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">Restart=on-abort</span></span><br><span class="line"></span><br><span class="line">[Install]</span><br><span class="line">WantedBy=multi-user.target</span><br><span class="line">EOF</span><br><span class="line"></span><br><span class="line">systemctl daemon-reload</span><br><span class="line">systemctl enable gbasedbtd</span><br><span class="line">systemctl stop gbasedbtd</span><br><span class="line">systemctl start gbasedbtd</span><br><span class="line">systemctl status gbasedbtd</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>



<p>$GBASEDBTDIR&#x2F;bin&#x2F;oninit.sh </p>
<p><strong>直接复制下面框体内容,然后创建oninit.sh文件即可,给予755权限.</strong></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">!/bin/bash</span></span><br><span class="line"></span><br><span class="line">cat /home/gbasedbt/.bash_profile  &gt; /tmp/bash_profile.tmp</span><br><span class="line">echo &quot;export GBASEDBTCONTIME=1&quot;  &gt;&gt; /tmp/bash_profile.tmp</span><br><span class="line">echo &quot;export GBASEDBTCONRETRY=0&quot; &gt;&gt; /tmp/bash_profile.tmp</span><br><span class="line">echo &quot;export GBASEDBTOFFSETS=1&quot;  &gt;&gt; /tmp/bash_profile.tmp</span><br><span class="line">echo &quot;export GBASEDBTSQLHOSTS=/tmp/sqlhsots.tmp&quot;  &gt;&gt; /tmp/bash_profile.tmp</span><br><span class="line">chmod 755 /tmp/bash_profile.tmp</span><br><span class="line"></span><br><span class="line">echo &quot;g330vip onsoctcp 192.168.1.13  9088&quot; &gt; /tmp/sqlhsots.tmp</span><br><span class="line"></span><br><span class="line">result=1</span><br><span class="line"></span><br><span class="line">objectServer=$(cat $GBASEDBTDIR/etc/sqlhosts |grep -vE &quot;$GBASEDBTSERVER|g330vip&quot;|awk &#x27;&#123;print $1&#125;&#x27;)</span><br><span class="line"></span><br><span class="line">vipdb=$(su - gbasedbt -c &quot;. /tmp/bash_profile.tmp &amp;&amp; echo &#x27;select 123456654321 from sysdual;&#x27; |dbaccess sysmaster@g330vip 2&gt;/dev/null |grep 123456654321 |wc -l&quot;)</span><br><span class="line"></span><br><span class="line">echo $vipdb</span><br><span class="line">if [ $vipdb -ne 1 ];then </span><br><span class="line">	su - gbasedbt -c &quot;onclean -ky&quot;</span><br><span class="line">	su - gbasedbt -c &quot;oninit -y&quot;</span><br><span class="line">	su - gbasedbt -c &quot;onmode -d primary $objectServer&quot;</span><br><span class="line">	result=$?</span><br><span class="line">fi</span><br><span class="line"></span><br><span class="line">if [ $vipdb -eq 1 ];then</span><br><span class="line">	su - gbasedbt -c &quot;onclean -ky&quot;</span><br><span class="line">	su - gbasedbt -c &quot;oninit -PHY&quot;</span><br><span class="line">	su - gbasedbt -c &quot;onmode -d secondary $objectServer&quot;</span><br><span class="line">	result=$?</span><br><span class="line">fi</span><br><span class="line"></span><br><span class="line">if [ $result -eq 0 ];then</span><br><span class="line">	/etc/init.d/keepalived start</span><br><span class="line">	result=$?</span><br><span class="line">fi</span><br><span class="line"></span><br><span class="line">exit $result</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>





<h2 id="关于GBase8s数据库的高可用方案小结"><a href="#关于GBase8s数据库的高可用方案小结" class="headerlink" title="关于GBase8s数据库的高可用方案小结"></a>关于GBase8s数据库的高可用方案小结</h2><ul>
<li><pre><code>    方案一: 数据库DRAUTO参数设置为0,用操作系统HA和共享存储RAID阵列保障数据库主节点高可用.从库只用作数据同步和查询使用.优点金融级别成熟可靠.缺点贵.优点故障的服务器不需要人工修复数据库。
</code></pre>
</li>
<li><pre><code>    方案二: 数据库DRAUTO参数设置为1或者2,业务用dbgroup组连接数据库集群组(group永远会透明连接到组中的主节点). 缺点主备间断网双主.缺点需要人工修复故障的服务器。
</code></pre>
</li>
<li><pre><code>    方案三: 数据库DRAUTO参数设置为3, 引入数据库的cm组件,通过连接管理器转发或代理连接.优点可以做透明的读写分离.缺点节点增多稳定性不够. 中性主备间断网数据同步中断,主和cm间断网双主.缺点需要人工修复故障的服务器。
</code></pre>
</li>
<li><pre><code>    方案四: 数据库DRAUTO参数设置为0, 引入keepalive组建,通过keepalive脚本检测数据库状态,提供数据库角色切换和vip切换.优点可以定制,有VIP. 缺点不如方案二简洁,缺点需要人工修复故障的服务器。
</code></pre>
</li>
</ul>
<h2 id="人工恢复故障节点"><a href="#人工恢复故障节点" class="headerlink" title="人工恢复故障节点"></a>人工恢复故障节点</h2><p> ontape.sh 恢复故障节点的数据,并将故障节点作为从库加入集群.在新主上执行入下命令:</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">在主机上用gbasedbt用户和执行下面两条命令: 192.168.1.12为故障节点的IP地址</span></span><br><span class="line">su - gbasedbt</span><br><span class="line"></span><br><span class="line">ontape -s -L 0 -t STDIO -F |ssh 192.168.1.12 &quot;. /home/gbasedbt/.bash_profile; onmode -ky;onclean -ky;ontape -p -t STDIO&quot;</span><br><span class="line"></span><br><span class="line">onmode -d primary g330hdr</span><br><span class="line"></span><br><span class="line">ssh 192.168.1.12 &quot;. /home/gbasedbt/.bash_profile; onmode -d secondary g330pri&quot;</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">GBase123!</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure> 
      <!-- reward -->
      
    </div>
    

    <!-- copyright -->
    
    <footer class="article-footer">
       
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/keeplived/" rel="tag">keeplived</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/%E8%87%AA%E7%A0%94/" rel="tag">自研</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/%E9%9B%86%E7%BE%A4/" rel="tag">集群</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/%E9%AB%98%E5%8F%AF%E7%94%A8/" rel="tag">高可用</a></li></ul>

    </footer>
  </div>

   
   
  
</article>

    
    <article
  id="post-如何在Ubuntu20-04上安装KVM"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h2 itemprop="name">
  <a class="article-title" href="/2023/04/21/%E5%A6%82%E4%BD%95%E5%9C%A8Ubuntu20-04%E4%B8%8A%E5%AE%89%E8%A3%85KVM/"
    >如何在Ubuntu20.04上安装KVM</a> 
</h2>
 

      
    </header>
     
    <div class="article-meta">
      <a href="/2023/04/21/%E5%A6%82%E4%BD%95%E5%9C%A8Ubuntu20-04%E4%B8%8A%E5%AE%89%E8%A3%85KVM/" class="article-date">
  <time datetime="2023-04-21T11:16:19.000Z" itemprop="datePublished">2023-04-21</time>
</a> 
  <div class="article-category">
    <a class="article-category-link" href="/categories/Linux/">Linux</a>
  </div>
   
    </div>
      
    <div class="article-entry" itemprop="articleBody">
       
  <p>KVM(基于内核的虚拟机)是一个内置于Linux内核的开源虚拟化技术。它允许你基于 Linux 或者 Windows 运行多个隔离的客户机虚拟机器。每一个客户机都拥有自己的操作系统和专用的虚拟硬件,例如 CPU(s), 内存, 网络接口和存储。</p>
<p>这篇指南提供了关于如何在 Ubuntu 20.04 上安装和配置 KVM 的指令。我们也会向你展示如何创建虚拟机，它们将被用作不同应用的开发环境。</p>
<h2 id="一、前提条件"><a href="#一、前提条件" class="headerlink" title="一、前提条件"></a>一、前提条件</h2><p>想要运行超过 2GB 内存的客户机，你必须拥有一个 64位主机系统。</p>
<p>在继续安装之前，确保你的 Ubuntu 主机支持 KVM 虚拟化。这个系统必须拥有 支持 VT-x（vmx）的 Intel 处理器 或者支持  AMD-V (svm) 技术的 AMD 处理器。</p>
<p>输入下面的<code>grep</code>命令来看看你的吹气是否支持硬件虚拟化：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">grep -<span class="title class_">Eoc</span> <span class="string">&#x27;(vmx|svm)&#x27;</span> /proc/cpuinfo</span><br></pre></td></tr></table></figure>

<p>如果你的 CPU 支持硬件虚拟化，这个命令将会打印出大于0的数字，这代表 CPU 核心数目。否则，如果输出为0，它意味着这个 CPU 不支持硬件虚拟化。</p>
<p>在一些机器上，虚拟化技术可能被厂商在 BIOS 中禁用了。</p>
<p>想要检查 VT 是否在 BIOS 中启用了，使用<code>kvm-ok</code>工具，它被包含在<code>cpu-checker</code>软件包。以 root 或者其他有 sudo 权限用户身份运行下面的命令，安装这个软件包：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">sudo apt update</span><br><span class="line">sudo apt install cpu-checker</span><br></pre></td></tr></table></figure>

<p>一旦安装完成，检查你的系统是否可以运行硬件加速的 KVM 虚拟机：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">kvm-ok</span><br></pre></td></tr></table></figure>

<p>如果处理器虚拟化能力没有在 BIOS 中被禁用，命令将会打印出:</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">INFO</span>: <span class="regexp">/dev/</span>kvm exists</span><br><span class="line"><span class="variable constant_">KVM</span> acceleration can be used</span><br></pre></td></tr></table></figure>

<p>否则，这个命令将会打印一个失败信息，和一个关于如何启用这个组件的简短消息。启用 AMD-V 或者 VT 技术的过程依赖于你的主板和处理器类型。根据你的主板文档信息来配置你的系统 BIOS。</p>
<h2 id="二、在-Ubuntu-20-04-上安装-KVM"><a href="#二、在-Ubuntu-20-04-上安装-KVM" class="headerlink" title="二、在 Ubuntu 20.04 上安装 KVM"></a>二、在 Ubuntu 20.04 上安装 KVM</h2><p>运行下面的命令安装 KVM，和额外的虚拟化管理软件包：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sudo apt install qemu-kvm libvirt-daemon-system libvirt-clients bridge-utils virtinst virt-manager</span><br></pre></td></tr></table></figure>

<ul>
<li><code>qemu-kvm</code> - 为 KVM 管理程序提供硬件模拟的软件程序</li>
<li><code>libvirt-daemon-system</code> - 将 libvirt 守护程序作为系统服务运行的配置文件</li>
<li><code>libvirt-clients</code> - 用来管理虚拟化平台的软件</li>
<li><code>bridge-utils</code> - 用来配置网络桥接的<a target="_blank" rel="noopener" href="https://cloud.tencent.com/product/cli?from=20065&from_column=20065">命令行工具</a></li>
<li><code>virtinst</code> - 用来创建虚拟机的命令行工具</li>
<li><code>virt-manager</code> - 提供一个易用的图形界面，并且通过libvirt 支持用于管理虚拟机的命令行工具</li>
</ul>
<p>一旦软件包被安装好，libvirt 守护程序将会自动启动。你可以通过运行下面的命令，验证它：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sudo systemctl is-active libvirtd</span><br></pre></td></tr></table></figure>

<p>输出：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">active</span><br></pre></td></tr></table></figure>

<p>想要创建和管理虚拟机，你需要添加你的用户到“libvirt” 和 “kvm” 用户组。输入：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">sudo usermod -aG libvirt $USER</span><br><span class="line">sudo usermod -aG kvm $USER</span><br></pre></td></tr></table></figure>

<p><code>$USER</code>是一个环境变量，用来记住当前登录用户的名字。</p>
<p>登出，并且重新登录，以便用户组被刷新。</p>
<h2 id="三、网络设置"><a href="#三、网络设置" class="headerlink" title="三、网络设置"></a>三、网络设置</h2><p>在libvirt 安装过程中，一个被称为 “virbr0”的桥接设备默认被创建。这个设备使用 NAT 来连接客户机到外面的世界。</p>
<p>运行 <code>brctl</code> 工具来列出当前的桥接和它们连接的接口：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">brctl show</span><br></pre></td></tr></table></figure>

<p>输出：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">bridge name	bridge id		      <span class="variable constant_">STP</span> enabled	interfaces</span><br><span class="line">virbr0		  <span class="number">8000.</span>52540089db3f	yes		      virbr0-nic</span><br></pre></td></tr></table></figure>

<p>“virbr0” 桥接没有添加任何的物理接口。“virbr0-nic”是一个虚拟设备，没有任何流量通过。这个设备唯一的目的就是避免修改“virbr0” 桥接的 MAC 地址。</p>
<p>网络设置适合大部分 Ubuntu 桌面用户，但是有限制。如果你想从外面的本地网络访问客户机，你需要创建一个新的桥接，并且配置它，以便客户机可以通过主机的物理接口连接到外部世界。</p>
<h2 id="四、创建虚拟机"><a href="#四、创建虚拟机" class="headerlink" title="四、创建虚拟机"></a>四、创建虚拟机</h2><p>现在 KVM 在你的 Ubuntu 桌面被安装，让我们一起去创建第一个虚拟机。我们可以从命令行或者使用<code>virt-manager</code>应用。</p>
<p>下载你想要安装的操作系统的 ISO 镜像并且按照下面的步骤来创建你的虚拟机：</p>
<p>01.在搜索栏输入“Virtual Machine Manager”并且点击图标来启动应用。</p>
<p>02.在应用启动后，从顶部菜单点击“File” -&gt; “New Virtual Machine”:</p>
<p>03.一个新窗口将会显示。选择  “Local install media” 并且点击  “Forward” 按钮。</p>
<p>04.提供你的 ISO 镜像路径，并且点击 Forward 按钮。</p>
<p>05.在下一个屏幕，选择虚拟机 VM 的内存和 CPU 设置。点 Forward。</p>
<p>06.下一步，选择“Create a disk image for the virtual machine” 并且选择虚拟机的磁盘空间大小。点击 Forward。</p>
<p>07.输入你的虚拟机名字并且点击 “Finish”。</p>
<p>08.虚拟机启动，并且一个新窗口将会打开：</p>
<p>从这里，你就可以按照屏幕的操作，去完成操作系统的安装。</p>
<p>一旦操作系统被安装好，你可以从<code>virt-manager</code>去访问它，使用 ssh 或者使用 Serial Console 接口。</p>
<h2 id="五、总结"><a href="#五、总结" class="headerlink" title="五、总结"></a>五、总结</h2><p>我们为你展示如何在 Ubuntu 20.04 系统上安装 KVM。现在你可以创建 Windows 或者 Linux 客户机。想要查找更多关于 KVM 的信息，浏览：<a target="_blank" rel="noopener" href="https://www.linux-kvm.org/page/Documents">KVM 文档页面</a>。</p>
<p>#Ubuntu20.04 网络br0配置<br><a target="_blank" rel="noopener" href="https://zhuanlan.zhihu.com/p/359864370">https://zhuanlan.zhihu.com/p/359864370</a><br><a target="_blank" rel="noopener" href="https://blog.csdn.net/qq_38916811/article/details/120792767">https://blog.csdn.net/qq_38916811/article/details/120792767</a><br>#kvm讲课<br><a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1qe4y127Hr?p=7&vd_source=ff2618562c52bd8088365be75a8e4c47">https://www.bilibili.com/video/BV1qe4y127Hr?p=7&amp;vd_source=ff2618562c52bd8088365be75a8e4c47</a><br>#Ubuntu20.04 网络配置<br><a target="_blank" rel="noopener" href="https://www.cnblogs.com/Gmiaomiao/p/14550151.html">https://www.cnblogs.com/Gmiaomiao/p/14550151.html</a></p>
<p>#查看虚拟机列表<br>virsh list –all<br>#创建虚拟磁盘<br>qemu-img create -f qcow2 &#x2F;kvm&#x2F;vdisk&#x2F;host91.qcow2 100G #如果是ssd用raw<br>#创建虚拟机<br>virt-install –name&#x3D;host91 <br>–vcpus&#x3D;2 –name&#x3D;2048 –disk&#x3D;&#x2F;kvm&#x2F;vdisk&#x2F;host91.qcow2 <br>–cdrom&#x3D;&#x2F;var&#x2F;lib&#x2F;libvirt&#x2F;images&#x2F;CentOS-7-x86_64-DVD-2003.iso <br>–os-variant&#x3D;rhel7 <br>–noautoconsole –autostart <br>–network bridge:br0 <br>–graphics vnc,listen&#x3D;0.0.0.0,prot&#x3D;5901</p>
<h1 id="Ubuntu20-04安装kvm并使用桥接网络-我的笔记"><a href="#Ubuntu20-04安装kvm并使用桥接网络-我的笔记" class="headerlink" title="Ubuntu20.04安装kvm并使用桥接网络(我的笔记)"></a>Ubuntu20.04安装kvm并使用桥接网络(我的笔记)</h1><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br></pre></td><td class="code"><pre><span class="line">1、更新 Ubuntu 22.04</span><br><span class="line">在一切开始前，打开终端并通过如下命令更新本地的软件包索引：</span><br><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">sudo apt update</span></span><br><span class="line"></span><br><span class="line">2、检查虚拟化是否开启</span><br><span class="line">在进一步行动之前，首先需要检查你的 CPU 是否支持 KVM 虚拟化，确保你系统中有 VT-x（ vmx）英特尔处理器或 AMD-V（svm）处理器。</span><br><span class="line">你可以通过运行如下命令，如果输出值大于 0，那么虚拟化被启用。否则，虚拟化被禁用，你需要启用它：</span><br><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">egrep -c <span class="string">&#x27;(vmx|svm)&#x27;</span> /proc/cpuinfo</span></span><br><span class="line">根据上方命令输出，你可以推断出虚拟化功能已经启用，因为输出结果大于 0。如果虚拟化功能没有启用，请确保在系统的 BIOS 设置中启用虚拟化功能。</span><br><span class="line">另外，你可以通过如下命令判断 KVM 虚拟化是否已经在运行：</span><br><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">sudo apt install -y cpu-checker</span></span><br><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">kvm-ok</span></span><br><span class="line">运行该命令之前，请确保你已经安装了 cpu-checker 软件包，否则将提示未找到该命令的报错。</span><br><span class="line">直接就在下面，你会得到如何解决这个问题的指示，那就是安装 cpu-checker 包。</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">3、在 Ubuntu 22.04 上安装 KVM</span><br><span class="line">随后，通过如下命令在 Ubuntu 22.04 中安装 KVM 以及其他相关虚拟化软件包：</span><br><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">sudo apt install -y qemu-kvm virt-manager libvirt-daemon-system virtinst libvirt-clients bridge-utils</span></span><br><span class="line">以下为你解释刚刚安装了哪些软件包：</span><br><span class="line">    qemu-kvm – 一个提供硬件仿真的开源仿真器和虚拟化包</span><br><span class="line">    virt-manager – 一款通过 libvirt 守护进程，基于 QT 的图形界面的虚拟机管理工具</span><br><span class="line">    libvirt-daemon-system – 为运行 libvirt 进程提供必要配置文件的工具</span><br><span class="line">    virtinst – 一套为置备和修改虚拟机提供的命令行工具</span><br><span class="line">    libvirt-clients – 一组客户端的库和API，用于从命令行管理和控制虚拟机和管理程序</span><br><span class="line">    bridge-utils – 一套用于创建和管理桥接设备的工具</span><br><span class="line">    </span><br><span class="line"> </span><br><span class="line"> 4、启用虚拟化守护进程（libvirtd）</span><br><span class="line">在所有软件包安装完毕之后，通过如下命令启用并启动 libvirt 守护进程：</span><br><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">sudo systemctl <span class="built_in">enable</span> --now libvirtd</span></span><br><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">sudo systemctl start libvirtd</span></span><br><span class="line">你可以通过如下命令验证该虚拟化守护进程是否已经运行：</span><br><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">sudo systemctl status libvirtd</span></span><br><span class="line">另外，请将当前登录用户加入 kvm 和 libvirt 用户组，以便能够创建和管理虚拟机。</span><br><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">sudo usermod -aG kvm <span class="variable">$USER</span></span></span><br><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">sudo usermod -aG libvirt <span class="variable">$USER</span></span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">5、创建网桥（br0）</span><br><span class="line">如果你打算从本机（Ubuntu 22.04）之外访问 KVM 虚拟机，你必须将虚拟机的网卡映射至网桥。virbr0 网桥是 KVM 安装完成后自动创建的，仅做测试用途。</span><br><span class="line">你可以通过如下内容在 /etc/netplan 目录下创建文件 01-netcfg.yaml 来新建网桥：</span><br><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">sudo vi /etc/netplan/01-netcfg.yaml</span></span><br><span class="line">network:</span><br><span class="line">  ethernets:</span><br><span class="line">    enp0s3:</span><br><span class="line">      dhcp4: false</span><br><span class="line">      dhcp6: false</span><br><span class="line"><span class="meta prompt_">  # </span><span class="language-bash">add configuration <span class="keyword">for</span> bridge interface</span></span><br><span class="line">  bridges:</span><br><span class="line">    br0:</span><br><span class="line">      interfaces: [enp0s3]</span><br><span class="line">      dhcp4: false</span><br><span class="line">      addresses: [192.168.1.162/24]</span><br><span class="line">      macaddress: 08:00:27:4b:1d:45</span><br><span class="line">      routes:</span><br><span class="line">        - to: default</span><br><span class="line">          via: 192.168.1.1</span><br><span class="line">          metric: 100</span><br><span class="line">      nameservers:</span><br><span class="line">        addresses: [4.2.2.2]</span><br><span class="line">      parameters:</span><br><span class="line">        stp: false</span><br><span class="line">      dhcp6: false</span><br><span class="line">  version: 2</span><br><span class="line">保存并退出文件。 </span><br><span class="line">--或者用我下面这个:</span><br><span class="line">pc@pc:/etc/netplan$ cat 00-installer-config.yaml </span><br><span class="line">network:</span><br><span class="line">  ethernets:</span><br><span class="line">    ens133:</span><br><span class="line">      dhcp4: false</span><br><span class="line">      dhcp6: false</span><br><span class="line">  bridges:</span><br><span class="line">    br0:</span><br><span class="line">      interfaces: [ ens133 ]</span><br><span class="line">      addresses: [192.168.1.100/24]</span><br><span class="line">      gateway4: 192.168.1.1</span><br><span class="line">      mtu: 1500</span><br><span class="line">      nameservers:</span><br><span class="line">        addresses: [8.8.8.8,8.8.4.4]</span><br><span class="line">      dhcp4: no</span><br><span class="line">      dhcp6: no</span><br><span class="line">  version: 2</span><br><span class="line"></span><br><span class="line">pc@pc:/etc/netplan$</span><br><span class="line">--说明: 物理网卡上面不要配置ip,配置在虚拟br0上</span><br><span class="line">pc@pc:/etc/netplan$ ip a</span><br><span class="line">1: lo: &lt;LOOPBACK,UP,LOWER_UP&gt; mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000</span><br><span class="line">    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00</span><br><span class="line">    inet 127.0.0.1/8 scope host lo</span><br><span class="line">       valid_lft forever preferred_lft forever</span><br><span class="line">    inet6 ::1/128 scope host </span><br><span class="line">       valid_lft forever preferred_lft forever</span><br><span class="line">2: ens133: &lt;BROADCAST,MULTICAST,UP,LOWER_UP&gt; mtu 1500 qdisc fq_codel master br0 state UP group default qlen 1000</span><br><span class="line">    link/ether 00:e0:4c:cd:24:71 brd ff:ff:ff:ff:ff:ff</span><br><span class="line">    altname enp3s0</span><br><span class="line">5: br0: &lt;BROADCAST,MULTICAST,UP,LOWER_UP&gt; mtu 1500 qdisc noqueue state UP group default qlen 1000</span><br><span class="line">    link/ether 00:e0:4c:cd:24:71 brd ff:ff:ff:ff:ff:ff</span><br><span class="line">    inet 192.168.1.100/24 brd 192.168.1.255 scope global noprefixroute br0</span><br><span class="line">       valid_lft forever preferred_lft forever</span><br><span class="line">    inet6 2409:8a4c:63a:d680:ebb1:953c:d6bb:b8da/64 scope global temporary dynamic </span><br><span class="line">       valid_lft 193986sec preferred_lft 85166sec</span><br><span class="line">    inet6 2409:8a4c:63a:d680:2e0:4cff:fecd:2471/64 scope global dynamic mngtmpaddr noprefixroute </span><br><span class="line">       valid_lft 193986sec preferred_lft 107586sec</span><br><span class="line">    inet6 fe80::2e0:4cff:fecd:2471/64 scope link noprefixroute </span><br><span class="line">       valid_lft forever preferred_lft forever</span><br><span class="line">6: vnet0: &lt;BROADCAST,MULTICAST,UP,LOWER_UP&gt; mtu 1500 qdisc fq_codel master br0 state UNKNOWN group default qlen 1000</span><br><span class="line">    link/ether fe:54:00:f0:04:66 brd ff:ff:ff:ff:ff:ff</span><br><span class="line">    inet6 fe80::fc54:ff:fef0:466/64 scope link </span><br><span class="line">       valid_lft forever preferred_lft forever</span><br><span class="line">pc@pc:/etc/netplan$ </span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">sudo netplan try</span></span><br><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">sudo netplan apply</span></span><br><span class="line">你可以通过如下 ip 命令，验证网桥 br0：</span><br><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">ip add show</span></span><br><span class="line">pc@pc:/etc/netplan$ route</span><br><span class="line">内核 IP 路由表</span><br><span class="line">目标            网关            子网掩码        标志  跃点   引用  使用 接口</span><br><span class="line">default         _gateway        0.0.0.0         UG    425    0        0 br0</span><br><span class="line">link-local      0.0.0.0         255.255.0.0     U     1000   0        0 ens133</span><br><span class="line">192.168.1.0     0.0.0.0         255.255.255.0   U     425    0        0 br0</span><br><span class="line">pc@pc:/etc/netplan$</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">virt-manager</span></span><br><span class="line"></span><br><span class="line">6、启动 KVM 虚拟机管理器</span><br><span class="line">当 KVM 安装完成后，你可以使用图形管理工具 virt-manager 创建虚拟机。你可以在 GNOME 搜索工具中搜索 Virtual Machine Manager 以启动。</span><br><span class="line">下面就算图形化安装,安装完成后也可以换桥接模式,通过详细界面选择</span><br><span class="line"></span><br><span class="line">7. VM是centos系统,配置静态IP</span><br><span class="line">[root@anonymous ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0 </span><br><span class="line">TYPE=Ethernet</span><br><span class="line">PROXY_METHOD=none</span><br><span class="line">BROWSER_ONLY=no</span><br><span class="line">BOOTPROTO=static</span><br><span class="line">DEFROUTE=yes</span><br><span class="line">IPV4_FAILURE_FATAL=no</span><br><span class="line">IPV6INIT=yes</span><br><span class="line">IPV6_AUTOCONF=yes</span><br><span class="line">IPV6_DEFROUTE=yes</span><br><span class="line">IPV6_FAILURE_FATAL=no</span><br><span class="line">IPV6_ADDR_GEN_MODE=stable-privacy</span><br><span class="line">NAME=eth0</span><br><span class="line">UUID=aa98c4ed-0cd5-48e3-ab58-32682358cb8a</span><br><span class="line">DEVICE=eth0</span><br><span class="line">ONBOOT=yes</span><br><span class="line"></span><br><span class="line">IPADDR=192.168.1.11</span><br><span class="line">NETMASK=255.255.255.0</span><br><span class="line">DNS=1114.114.114.114</span><br><span class="line">GATEWAY=192.168.1.1</span><br><span class="line"></span><br><span class="line">[root@anonymous ~]#</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">小结: </span><br><span class="line">1, ubuntu使用了netplan管理网络</span><br><span class="line">2, 宿主机添加好桥接网卡后,能通过桥接网卡上网后在安装vm</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure> 
      <!-- reward -->
      
    </div>
    

    <!-- copyright -->
    
    <footer class="article-footer">
       
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/KVM/" rel="tag">KVM</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Ubuntu/" rel="tag">Ubuntu</a></li></ul>

    </footer>
  </div>

   
   
  
</article>

    
    <article
  id="post-python人工智能学习环境搭建"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h2 itemprop="name">
  <a class="article-title" href="/2023/04/20/python%E4%BA%BA%E5%B7%A5%E6%99%BA%E8%83%BD%E5%AD%A6%E4%B9%A0%E7%8E%AF%E5%A2%83%E6%90%AD%E5%BB%BA/"
    >python人工智能学习环境搭建</a> 
</h2>
 

      
    </header>
     
    <div class="article-meta">
      <a href="/2023/04/20/python%E4%BA%BA%E5%B7%A5%E6%99%BA%E8%83%BD%E5%AD%A6%E4%B9%A0%E7%8E%AF%E5%A2%83%E6%90%AD%E5%BB%BA/" class="article-date">
  <time datetime="2023-04-20T11:38:31.000Z" itemprop="datePublished">2023-04-20</time>
</a> 
  <div class="article-category">
    <a class="article-category-link" href="/categories/Python/">Python</a>
  </div>
   
    </div>
      
    <div class="article-entry" itemprop="articleBody">
       
  <h2 id="PC-Linux-python-AI环境"><a href="#PC-Linux-python-AI环境" class="headerlink" title="PC-Linux-python AI环境"></a>PC-Linux-python AI环境</h2><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash"><span class="comment">## python国内</span></span></span><br><span class="line">pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash"><span class="comment">## python虚拟环境(AI)</span></span></span><br><span class="line">pip3 install virtualenv</span><br><span class="line">pip3 install virtualenvwrapper </span><br><span class="line">export WORKON_HOME=&quot;~/.virtualenvs&quot;    </span><br><span class="line">export VIRTUALENVWRAPPER_PYTHON=&quot;/usr/bin/python3&quot;    </span><br><span class="line">source /usr/local/bin/virtualenvwrapper.sh</span><br><span class="line">mkvirtualenv ai  #创建虚拟环境</span><br><span class="line">deactivate       #退出虚拟环境</span><br><span class="line">workon           #列出虚拟环境</span><br><span class="line">workon           #进入虚拟环境</span><br><span class="line">apt-get install pkg-config </span><br><span class="line">sudo apt-get install libfreetype6-dev libxft-dev</span><br><span class="line">cat &gt; requirements.txt &lt;&lt; EOF</span><br><span class="line">matplotlib</span><br><span class="line">numpy</span><br><span class="line">pandas</span><br><span class="line">tables</span><br><span class="line">jupyter</span><br><span class="line">EOF</span><br><span class="line">(ai) pc@pc:~/wangwei2023/src$ pip install -r requirements.txt </span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>



<h3 id="Windows参考"><a href="#Windows参考" class="headerlink" title="Windows参考"></a>Windows参考</h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">cd F:\gitee.com\wangwei830\python\ai</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">文件 requirements.txt</span> </span><br><span class="line">matplotlib==2.2.2</span><br><span class="line">numpy==1.14.2</span><br><span class="line">pandas==0.20.3</span><br><span class="line">tables==3.4.2</span><br><span class="line">jupyter==1.0.0</span><br><span class="line"></span><br><span class="line">pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple/</span><br><span class="line">pip install virtualenv</span><br><span class="line">pip install virtualenvwrapper-win</span><br><span class="line">mkvirtualenv ai</span><br><span class="line">pip install -r requirements.txt</span><br><span class="line"></span><br></pre></td></tr></table></figure>



<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">1. 依次执行</span></span><br><span class="line"></span><br><span class="line">​```</span><br><span class="line">pip install virtualenv</span><br><span class="line">pip install virtualenvwrapper-win</span><br><span class="line">​```</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">2.创建python虚拟环境</span></span><br><span class="line"></span><br><span class="line">​```</span><br><span class="line">mkvirtualenv [虚拟环境名称]</span><br><span class="line">​```</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">3.使用pip命令安装各版本库</span></span><br><span class="line"></span><br><span class="line">​```</span><br><span class="line">pip3 install -r jupyter=1.0.0</span><br><span class="line">​```</span><br><span class="line"></span><br><span class="line">`pip3 install -r 文件名.txt`</span><br><span class="line"> 注：可以把各版本的库，放到一个文件中，最好安装稳定的版本</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">4.退出虚拟环境</span></span><br><span class="line"></span><br><span class="line">​```</span><br><span class="line">deactivate</span><br><span class="line">​```</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">5.删除（一般不要使用）</span></span><br><span class="line"></span><br><span class="line">​```</span><br><span class="line">rmvirtualenv [虚拟环境名称]</span><br><span class="line">​```</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">6.打开虚拟环境</span></span><br><span class="line"></span><br><span class="line">​```</span><br><span class="line">worken [虚拟环境名称]</span><br><span class="line">​```</span><br><span class="line"></span><br><span class="line">注：虚拟环境可以直接在C盘创建</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>



 
      <!-- reward -->
      
    </div>
    

    <!-- copyright -->
    
    <footer class="article-footer">
       
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/%E5%9F%BA%E7%A1%80/" rel="tag">基础</a></li></ul>

    </footer>
  </div>

   
   
  
</article>

    
    <article
  id="post-golang统计中文字符和和英文字符"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h2 itemprop="name">
  <a class="article-title" href="/2023/04/19/golang%E7%BB%9F%E8%AE%A1%E4%B8%AD%E6%96%87%E5%AD%97%E7%AC%A6%E5%92%8C%E5%92%8C%E8%8B%B1%E6%96%87%E5%AD%97%E7%AC%A6/"
    >golang统计中文字符和和英文字符</a> 
</h2>
 

      
    </header>
     
    <div class="article-meta">
      <a href="/2023/04/19/golang%E7%BB%9F%E8%AE%A1%E4%B8%AD%E6%96%87%E5%AD%97%E7%AC%A6%E5%92%8C%E5%92%8C%E8%8B%B1%E6%96%87%E5%AD%97%E7%AC%A6/" class="article-date">
  <time datetime="2023-04-19T12:55:13.000Z" itemprop="datePublished">2023-04-19</time>
</a> 
  <div class="article-category">
    <a class="article-category-link" href="/categories/Golang/">Golang</a>
  </div>
   
    </div>
      
    <div class="article-entry" itemprop="articleBody">
       
  <h3 id="识别字符串中的中文"><a href="#识别字符串中的中文" class="headerlink" title="识别字符串中的中文"></a>识别字符串中的中文</h3><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> main</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> <span class="string">&quot;fmt&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">	s := <span class="string">&quot;a中b国c&quot;</span></span><br><span class="line">	fmt.Println(<span class="string">&quot;ruange也是直接取字符：----------------------&quot;</span>)</span><br><span class="line">	<span class="keyword">for</span> i := <span class="keyword">range</span> s &#123;</span><br><span class="line">		fmt.Println(i, s[i])</span><br><span class="line">	&#125;</span><br><span class="line">	fmt.Println(<span class="string">&quot;ruange也是直接取字符：----------------------&quot;</span>)</span><br><span class="line">	<span class="keyword">for</span> i, c := <span class="keyword">range</span> s &#123;</span><br><span class="line">		fmt.Println(i, c)</span><br><span class="line">	&#125;</span><br><span class="line">	fmt.Println(<span class="string">&quot;字节数：len(s)----------------------&quot;</span>)</span><br><span class="line">	<span class="keyword">for</span> i := <span class="number">0</span>; i &lt; <span class="built_in">len</span>(s); i++ &#123;</span><br><span class="line">		fmt.Println(i, s[i])</span><br><span class="line">	&#125;</span><br><span class="line">	fmt.Println(<span class="string">&quot;字符数：len([]rune(s))----------------------&quot;</span>)</span><br><span class="line">	<span class="keyword">for</span> i := <span class="number">0</span>; i &lt; <span class="built_in">len</span>([]<span class="type">rune</span>(s)); i++ &#123;</span><br><span class="line">		fmt.Println(i, []<span class="type">rune</span>(s)[i])</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>





<h3 id="go的基本数据类型"><a href="#go的基本数据类型" class="headerlink" title="go的基本数据类型"></a>go的基本数据类型</h3><ul>
<li>布尔型<br>bool 布尔型的值只可以是常量 true 或者 false，默认值为 false。</li>
<li>字符串类型<br>string 编码统一为 UTF-8 编码标识 Unicode 文本，默认值为空字符串。</li>
<li>整型(默认值为 0)<br>uint8：无符号 8 位整型(0 ~ 255)；<br>uint16：无符号 16 位整型(0 ~ 65535)；<br>uint32：无符号 32 位整型(0 ~ 4294967295)；<br>uint64：无符号 64 位整型(0 ~ 18446744073709551615)；<br>int8：有符号 8 位整型(-128 ~ 127)；<br>int16：有符号 16 位整型(-32768 ~ 32767)；<br>int32：有符号 32 位整型(-2147483648 ~ 2147483647)；<br>int64：有符号 64 位整型(-9223372036854775808 ~ 9223372036854775807)</li>
<li>浮点型(默认值为 0)<br>float32：IEEE-754 32 位浮点数；<br>float64：IEEE-754 64 位浮点数；<br>complex64：32 位实数和虚数；<br>complex128：64 位实数和虚数；</li>
<li>其他数值类型<br>byte：类似 uint8；<br>rune：类似 int32；<br>uint：32 或 64 位；<br>int：与 uint 一样大小；<br>uintptr：无符号整型，用于存放一个指针；</li>
</ul>
 
      <!-- reward -->
      
    </div>
    

    <!-- copyright -->
    
    <footer class="article-footer">
       
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/%E4%B8%AD%E6%96%87%E5%AD%97%E7%AC%A6/" rel="tag">中文字符</a></li></ul>

    </footer>
  </div>

   
   
  
</article>

    
    <article
  id="post-使用特殊字符作为分隔符delimiter"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h2 itemprop="name">
  <a class="article-title" href="/2023/04/18/%E4%BD%BF%E7%94%A8%E7%89%B9%E6%AE%8A%E5%AD%97%E7%AC%A6%E4%BD%9C%E4%B8%BA%E5%88%86%E9%9A%94%E7%AC%A6delimiter/"
    >使用特殊字符作为分隔符delimiter</a> 
</h2>
 

      
    </header>
     
    <div class="article-meta">
      <a href="/2023/04/18/%E4%BD%BF%E7%94%A8%E7%89%B9%E6%AE%8A%E5%AD%97%E7%AC%A6%E4%BD%9C%E4%B8%BA%E5%88%86%E9%9A%94%E7%AC%A6delimiter/" class="article-date">
  <time datetime="2023-04-18T14:59:44.000Z" itemprop="datePublished">2023-04-18</time>
</a> 
  <div class="article-category">
    <a class="article-category-link" href="/categories/GBase8s/">GBase8s</a>
  </div>
   
    </div>
      
    <div class="article-entry" itemprop="articleBody">
       
  <h3 id="使用字符-x1c作为分隔符"><a href="#使用字符-x1c作为分隔符" class="headerlink" title="使用字符\x1c作为分隔符"></a>使用字符\x1c作为分隔符</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">&gt; select * from t1;</span><br><span class="line">         id name</span><br><span class="line">          1 aa</span><br><span class="line">          1 aa</span><br><span class="line"></span><br><span class="line">[gbasedbt@wei07 ~]$ awk &#x27; BEGIN &#123;print &quot;unload to /tmp/a.unl2 delimiter \x27\x1c\x27 select * from t1&quot;&#125;&#x27;|dbaccess appdb</span><br><span class="line"></span><br><span class="line">Database selected.</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">2 row(s) unloaded.</span><br><span class="line"></span><br><span class="line">Elapsed time: 0.004 sec</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">Database closed.</span><br><span class="line"></span><br><span class="line">[gbasedbt@wei07 ~]$</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="查看导出的结果"><a href="#查看导出的结果" class="headerlink" title="查看导出的结果"></a>查看导出的结果</h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">[gbasedbt@wei07 ~]$ cat /tmp/a.unl2</span><br><span class="line">1aa</span><br><span class="line">1aa</span><br><span class="line">[gbasedbt@wei07 ~]$ cat -A /tmp/a.unl2</span><br><span class="line">1^\aa^\$</span><br><span class="line">1^\aa^\$</span><br><span class="line">[gbasedbt@wei07 ~]$</span><br><span class="line">[gbasedbt@wei07 ~]$ xxd /tmp/a.unl2</span><br><span class="line">0000000: 311c 6161 1c0a 311c 6161 1c0a            1.aa..1.aa..</span><br><span class="line">[gbasedbt@wei07 ~]$</span><br></pre></td></tr></table></figure>

<h3 id="gbasedbt不允许的分隔符有哪些"><a href="#gbasedbt不允许的分隔符有哪些" class="headerlink" title="gbasedbt不允许的分隔符有哪些"></a>gbasedbt不允许的分隔符有哪些</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">Use the DELIMITER clause to specify the delimiter that separates the data contained in each column in a row in the input file. You can specify TAB (CTRL-I) or a blank space (= ASCII 32) as the delimiter symbol. You cannot use the following items as the delimiter symbol:</span><br><span class="line"></span><br><span class="line">    Backslash ( \ )</span><br><span class="line">    NEWLINE character (CTRL-J)</span><br><span class="line">    Hexadecimal numbers (0 to 9, a to f, A to F)</span><br><span class="line"></span><br><span class="line">If you omit this clause, the database server checks the DBDELIMITER environment variable. For information about how to set the DBDELIMITER environment variable, see the IBM® Informix® Guide to SQL: Reference.</span><br><span class="line"></span><br><span class="line">If the DBDELIMITER environment variable has not been set, the default delimiter is the pipe ( | ).</span><br><span class="line">The following example specifies the semicolon ( ; ) as the delimiting character. The example uses Windows file-naming conventions. </span><br></pre></td></tr></table></figure> 
      <!-- reward -->
      
    </div>
    

    <!-- copyright -->
    
    <footer class="article-footer">
       
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/%E5%88%86%E9%9A%94%E7%AC%A6/" rel="tag">分隔符</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/%E8%BF%81%E7%A7%BB/" rel="tag">迁移</a></li></ul>

    </footer>
  </div>

   
   
  
</article>

    
    <article
  id="post-C语言：字符串基础知识笔记"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h2 itemprop="name">
  <a class="article-title" href="/2023/04/15/C%E8%AF%AD%E8%A8%80%EF%BC%9A%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86%E7%AC%94%E8%AE%B0/"
    >C语言：字符串基础知识笔记</a> 
</h2>
 

      
    </header>
     
    <div class="article-meta">
      <a href="/2023/04/15/C%E8%AF%AD%E8%A8%80%EF%BC%9A%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86%E7%AC%94%E8%AE%B0/" class="article-date">
  <time datetime="2023-04-15T14:19:56.000Z" itemprop="datePublished">2023-04-15</time>
</a> 
  <div class="article-category">
    <a class="article-category-link" href="/categories/C%E8%AF%AD%E8%A8%80/">C语言</a>
  </div>
   
    </div>
      
    <div class="article-entry" itemprop="articleBody">
       
  <h3 id=""><a href="#" class="headerlink" title=""></a></h3><hr>
<h3 id="字符串基础"><a href="#字符串基础" class="headerlink" title="字符串基础"></a>字符串基础</h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;string.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">main</span><span class="params">()</span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//原始字符串</span></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;原始字符串---------------------------------\n&quot;</span>);</span><br><span class="line">    <span class="type">char</span> str1[] = <span class="string">&quot;abcd&quot;</span>;</span><br><span class="line">    <span class="type">char</span> str2[<span class="number">50</span>] = <span class="string">&quot;123456&quot;</span> ;</span><br><span class="line">    <span class="type">char</span> str3[<span class="number">5</span>] ;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;str1: %20s,sizeof: %20d,strlen: %20d\n&quot;</span>,str1,<span class="keyword">sizeof</span>(str1),<span class="built_in">strlen</span>(str1));</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;str2: %20s,sizeof: %20d,strlen: %20d\n&quot;</span>,str2,<span class="keyword">sizeof</span>(str2),<span class="built_in">strlen</span>(str2));</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;str3: %20s,sizeof: %20d,strlen: %20d\n&quot;</span>,str3,<span class="keyword">sizeof</span>(str3),<span class="built_in">strlen</span>(str3));</span><br><span class="line"></span><br><span class="line">    <span class="comment">//字符串复制</span></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;字符串复制---------------------------------\n&quot;</span>);</span><br><span class="line">    <span class="built_in">strncpy</span>(str2,str1,<span class="number">2</span>);</span><br><span class="line">    str2[<span class="number">2</span>]=<span class="string">&#x27;\0&#x27;</span>;      <span class="comment">//只能短的向长的复制，不然有越界</span></span><br><span class="line">    <span class="built_in">strcpy</span>(str3,str1);   </span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;str1: %20s,sizeof: %20d,strlen: %20d\n&quot;</span>,str1,<span class="keyword">sizeof</span>(str1),<span class="built_in">strlen</span>(str1));</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;str2: %20s,sizeof: %20d,strlen: %20d\n&quot;</span>,str2,<span class="keyword">sizeof</span>(str2),<span class="built_in">strlen</span>(str2));</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;str3: %20s,sizeof: %20d,strlen: %20d\n&quot;</span>,str3,<span class="keyword">sizeof</span>(str3),<span class="built_in">strlen</span>(str3));</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">//字符串连接</span></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;字符串连接---------------------------------\n&quot;</span>);</span><br><span class="line">    <span class="built_in">strcat</span>(str2,<span class="string">&quot;999&quot;</span>);</span><br><span class="line">    <span class="built_in">strcat</span>(str2,str1); <span class="comment">//注意不要越界</span></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;str1: %20s,sizeof: %20d,strlen: %20d\n&quot;</span>,str1,<span class="keyword">sizeof</span>(str1),<span class="built_in">strlen</span>(str1));</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;str2: %20s,sizeof: %20d,strlen: %20d\n&quot;</span>,str2,<span class="keyword">sizeof</span>(str2),<span class="built_in">strlen</span>(str2));</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;str3: %20s,sizeof: %20d,strlen: %20d\n&quot;</span>,str3,<span class="keyword">sizeof</span>(str3),<span class="built_in">strlen</span>(str3));</span><br><span class="line"></span><br><span class="line">    <span class="comment">//字符串比较</span></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;字符串比较---------------------------------\n&quot;</span>);</span><br><span class="line">    <span class="built_in">strcpy</span>(str1,<span class="string">&quot;abc&quot;</span>);</span><br><span class="line">    <span class="built_in">strcpy</span>(str2,<span class="string">&quot;acc&quot;</span>);</span><br><span class="line">    <span class="type">int</span> jj = <span class="built_in">strcmp</span>(str1,str2);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;diff: %d\n&quot;</span>,jj);</span><br><span class="line">    <span class="keyword">if</span> (jj==<span class="number">0</span>) &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;两个字符串相同&quot;</span>);</span><br><span class="line">    &#125; </span><br><span class="line">    <span class="keyword">else</span> <span class="keyword">if</span> (jj&gt;<span class="number">0</span>) &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;str1的asc码比str2大&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">else</span>&#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;str1的asc码比str2小&quot;</span>);</span><br><span class="line">    &#125;   </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="运行结果"><a href="#运行结果" class="headerlink" title="运行结果"></a>运行结果</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">[Running] cd &quot;d:\soft\src\Python3\C\&quot; &amp;&amp; gcc a.c -o a &amp;&amp; &quot;d:\soft\src\Python3\C\&quot;a</span><br><span class="line">原始字符串---------------------------------</span><br><span class="line">str1:                 abcd,sizeof:                    5,strlen:                    4</span><br><span class="line">str2:               123456,sizeof:                   50,strlen:                    6</span><br><span class="line">str3:                     ,sizeof:                    5,strlen:                    0</span><br><span class="line">字符串复制---------------------------------</span><br><span class="line">str1:                 abcd,sizeof:                    5,strlen:                    4</span><br><span class="line">str2:                   ab,sizeof:                   50,strlen:                    2</span><br><span class="line">str3:                 abcd,sizeof:                    5,strlen:                    4</span><br><span class="line">字符串连接---------------------------------</span><br><span class="line">str1:                 abcd,sizeof:                    5,strlen:                    4</span><br><span class="line">str2:            ab999abcd,sizeof:                   50,strlen:                    9</span><br><span class="line">str3:                 abcd,sizeof:                    5,strlen:                    4</span><br><span class="line">字符串比较---------------------------------</span><br><span class="line">diff: -1</span><br><span class="line">str1的asc码比str2小</span><br><span class="line">[Done] exited with code=23 in 0.332 seconds</span><br></pre></td></tr></table></figure> 
      <!-- reward -->
      
    </div>
    

    <!-- copyright -->
    
    <footer class="article-footer">
       
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/%E5%AD%97%E7%AC%A6%E4%B8%B2/" rel="tag">字符串</a></li></ul>

    </footer>
  </div>

   
   
  
</article>

    
    <article
  id="post-C语言：指针数组和数组指针笔记"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h2 itemprop="name">
  <a class="article-title" href="/2023/04/15/C%E8%AF%AD%E8%A8%80%EF%BC%9A%E6%8C%87%E9%92%88%E6%95%B0%E7%BB%84%E5%92%8C%E6%95%B0%E7%BB%84%E6%8C%87%E9%92%88%E7%AC%94%E8%AE%B0/"
    >C语言：指针数组和数组指针笔记</a> 
</h2>
 

      
    </header>
     
    <div class="article-meta">
      <a href="/2023/04/15/C%E8%AF%AD%E8%A8%80%EF%BC%9A%E6%8C%87%E9%92%88%E6%95%B0%E7%BB%84%E5%92%8C%E6%95%B0%E7%BB%84%E6%8C%87%E9%92%88%E7%AC%94%E8%AE%B0/" class="article-date">
  <time datetime="2023-04-15T14:19:56.000Z" itemprop="datePublished">2023-04-15</time>
</a> 
  <div class="article-category">
    <a class="article-category-link" href="/categories/C%E8%AF%AD%E8%A8%80/">C语言</a>
  </div>
   
    </div>
      
    <div class="article-entry" itemprop="articleBody">
       
  <h3 id="代码"><a href="#代码" class="headerlink" title="代码"></a>代码</h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span>&#123;</span><br><span class="line">    <span class="comment">// 指针基础</span></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;\n指针基础----------------------------- \n&quot;</span>);</span><br><span class="line">    <span class="type">char</span> c = <span class="string">&#x27;a&#x27;</span>;</span><br><span class="line">    <span class="type">int</span> i = <span class="number">123</span>;</span><br><span class="line">    <span class="type">char</span> *pc = &amp;c;</span><br><span class="line">    <span class="type">int</span> *pi = &amp;i;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;c 的地址 %p , 指针pc的地址 %p ，指针pc的值 %p, 指针pc的解引用 %c \n&quot;</span>,&amp;c,&amp;pc,pc,*pc);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;i 的地址 %p , 指针pi的地址 %p ，指针pi的值 %p, 指针pi的解引用 %d \n&quot;</span>,&amp;i,&amp;pi,pi,*pi);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;指针大大小 %d 字节.\n&quot;</span>,<span class="keyword">sizeof</span>(pc));</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;指针大大小 %d 字节.\n&quot;</span>,<span class="keyword">sizeof</span>(pi));</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">// 使用指针基础</span></span><br><span class="line">    <span class="type">char</span> lst01[] = &#123;<span class="string">&#x27;a&#x27;</span>,<span class="string">&#x27;b&#x27;</span>,<span class="string">&#x27;c&#x27;</span>&#125;;</span><br><span class="line">    <span class="type">char</span> *pchar = lst01;</span><br><span class="line">    <span class="keyword">for</span> (i=<span class="number">0</span>;i&lt;<span class="number">3</span>;i++)&#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;pchar指针指向的地址 %p,解引用 %c\n&quot;</span>,pchar+i,*(pchar+i));</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 使用指针基础升级</span></span><br><span class="line">    <span class="type">char</span> (*pchar2)[<span class="number">3</span>] = &amp;lst01;</span><br><span class="line">    <span class="keyword">for</span> (i=<span class="number">0</span>;i&lt;<span class="number">3</span>;i++)&#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;pchar2指针指向的数据的元素%d的地址 %p,解引用 %c\n&quot;</span>,i,*pchar2+i,*(*pchar2+i));</span><br><span class="line">        <span class="comment">//说明：解引用，去掉一层壳。</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//说明 [] 的优先级比 * 高，先入位主。  *p[] 是数组；(*p)[] 是指针。 </span></span><br><span class="line">    <span class="comment">// char *p[]   : 指针数组，即：数组，存储一连串指针的数组，同时这些指针都指向char类型。</span></span><br><span class="line">    <span class="comment">// char (*p)[] : 数组指针，即：指针，存储一个指针，指向一个char类型的数组。</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//指针数组 -- 指针例子1  （说明：汉语后入为主）</span></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;\n指针的数组 -- 例子1----------------------------- \n&quot;</span>);</span><br><span class="line">    <span class="type">char</span> a = <span class="string">&#x27;a&#x27;</span>;</span><br><span class="line">    <span class="type">char</span> b = <span class="string">&#x27;b&#x27;</span>;</span><br><span class="line">    <span class="type">char</span> *p1 = &amp;a;</span><br><span class="line">    <span class="type">char</span> *p2 = &amp;b;</span><br><span class="line">    <span class="type">char</span> *A1[<span class="number">2</span>] = &#123;p1,p2&#125;;</span><br><span class="line">    <span class="type">int</span> n = <span class="number">0</span> ;</span><br><span class="line">    <span class="keyword">for</span> (n=<span class="number">0</span>;n&lt;<span class="number">2</span>;n++)&#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;数组A1的 %d 个元素:  地址是：%p  ，值是：%p ， 解引用是：%c\n&quot;</span>,n+<span class="number">1</span>, &amp;A1[n], A1[n], *A1[n]);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//指针数组 -- 数组例子2</span></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;指针的数组 -- 例子2----------------------------- \n&quot;</span>);</span><br><span class="line">    <span class="type">char</span> *A2[] = &#123;<span class="string">&quot;abc&quot;</span>,<span class="string">&quot;ABC&quot;</span>,<span class="string">&quot;123&quot;</span>&#125;;</span><br><span class="line">    <span class="keyword">for</span> (n=<span class="number">0</span>;n&lt;<span class="number">3</span>;n++)&#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;数组A2的 %d 个元素:  地址是：%p  ，值是：%p ， 解引用是：%s\n&quot;</span>,n+<span class="number">1</span>, &amp;A2[n], A2[n], A2[n]);</span><br><span class="line">        <span class="comment">//说明：打印字符串值需要的是字符串的地址，而不是*A2[n]解引用这个地址。</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//数组指针 -- 指针</span></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;\n数组指针 ------------------------------- \n&quot;</span>);</span><br><span class="line">    <span class="type">int</span> l[] = &#123;<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>&#125;;</span><br><span class="line">    <span class="type">int</span> (*P)[<span class="number">5</span>] = &amp;l;</span><br><span class="line">    <span class="type">int</span> m=<span class="number">0</span>;</span><br><span class="line">    <span class="keyword">for</span> (m=<span class="number">0</span>;m&lt;<span class="number">5</span>;m++)&#123;</span><br><span class="line">        <span class="comment">//用指针偏移</span></span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;用指针偏移：指针指向的数组地址是 %p；  数组的第 %d 个元素地址%p 值是%d\n&quot;</span>, P,m+<span class="number">1</span>,*P+m,*(*P+m));</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">for</span> (m=<span class="number">0</span>;m&lt;<span class="number">5</span>;m++)&#123;</span><br><span class="line">        <span class="comment">//用数组下标</span></span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;用数组下标：指针指向的数组地址是 %p；  数组的第 %d 个元素地址%p 值是%d\n&quot;</span>, P,m+<span class="number">1</span>,&amp;(*P)[m] , (*P)[m]);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="结果"><a href="#结果" class="headerlink" title="结果"></a>结果</h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line">[Running] cd &quot;d:\soft\src\Python3\C\&quot; &amp;&amp; gcc a.c -o a &amp;&amp; &quot;d:\soft\src\Python3\C\&quot;a</span><br><span class="line"></span><br><span class="line">指针基础----------------------------- </span><br><span class="line">c 的地址 00000018b25ffd0f , 指针pc的地址 00000018b25ffd00 ，指针pc的值 00000018b25ffd0f, 指针pc的解引用 a </span><br><span class="line">i 的地址 00000018b25ffd08 , 指针pi的地址 00000018b25ffcf8 ，指针pi的值 00000018b25ffd08, 指针pi的解引用 123 </span><br><span class="line">指针大大小 8 字节.</span><br><span class="line">指针大大小 8 字节.</span><br><span class="line">pchar指针指向的地址 00000018b25ffcf5,解引用 a</span><br><span class="line">pchar指针指向的地址 00000018b25ffcf6,解引用 b</span><br><span class="line">pchar指针指向的地址 00000018b25ffcf7,解引用 c</span><br><span class="line">pchar2指针指向的数据的元素0的地址 00000018b25ffcf5,解引用 a</span><br><span class="line">pchar2指针指向的数据的元素1的地址 00000018b25ffcf6,解引用 b</span><br><span class="line">pchar2指针指向的数据的元素2的地址 00000018b25ffcf7,解引用 c</span><br><span class="line"></span><br><span class="line">指针的数组 -- 例子1----------------------------- </span><br><span class="line">数组A1的 1 个元素:  地址是：00000018b25ffce0  ，值是：00000018b25ffcf4 ， 解引用是：a</span><br><span class="line">数组A1的 2 个元素:  地址是：00000018b25ffce8  ，值是：00000018b25ffcf3 ， 解引用是：b</span><br><span class="line">指针的数组 -- 例子2----------------------------- </span><br><span class="line">数组A2的 1 个元素:  地址是：00000018b25ffcc0  ，值是：00007ff7d641a24a ， 解引用是：abc</span><br><span class="line">数组A2的 2 个元素:  地址是：00000018b25ffcc8  ，值是：00007ff7d641a24e ， 解引用是：ABC</span><br><span class="line">数组A2的 3 个元素:  地址是：00000018b25ffcd0  ，值是：00007ff7d641a252 ， 解引用是：123</span><br><span class="line"></span><br><span class="line">数组指针 ------------------------------- </span><br><span class="line">用指针偏移：指针指向的数组地址是 00000018b25ffca0；  数组的第 1 个元素地址00000018b25ffca0 值是1</span><br><span class="line">用指针偏移：指针指向的数组地址是 00000018b25ffca0；  数组的第 2 个元素地址00000018b25ffca4 值是2</span><br><span class="line">用指针偏移：指针指向的数组地址是 00000018b25ffca0；  数组的第 3 个元素地址00000018b25ffca8 值是3</span><br><span class="line">用指针偏移：指针指向的数组地址是 00000018b25ffca0；  数组的第 4 个元素地址00000018b25ffcac 值是4</span><br><span class="line">用指针偏移：指针指向的数组地址是 00000018b25ffca0；  数组的第 5 个元素地址00000018b25ffcb0 值是5</span><br><span class="line">用数组下标：指针指向的数组地址是 00000018b25ffca0；  数组的第 1 个元素地址00000018b25ffca0 值是1</span><br><span class="line">用数组下标：指针指向的数组地址是 00000018b25ffca0；  数组的第 2 个元素地址00000018b25ffca4 值是2</span><br><span class="line">用数组下标：指针指向的数组地址是 00000018b25ffca0；  数组的第 3 个元素地址00000018b25ffca8 值是3</span><br><span class="line">用数组下标：指针指向的数组地址是 00000018b25ffca0；  数组的第 4 个元素地址00000018b25ffcac 值是4</span><br><span class="line">用数组下标：指针指向的数组地址是 00000018b25ffca0；  数组的第 5 个元素地址00000018b25ffcb0 值是5</span><br><span class="line"></span><br><span class="line">[Done] exited with code=0 in 0.315 seconds</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

 
      <!-- reward -->
      
    </div>
    

    <!-- copyright -->
    
    <footer class="article-footer">
       
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/%E6%8C%87%E9%92%88%E6%95%B0%E7%BB%84/" rel="tag">指针数组</a></li></ul>

    </footer>
  </div>

   
   
  
</article>

    
    <article
  id="post-Linux-device-mapper"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h2 itemprop="name">
  <a class="article-title" href="/2023/04/15/Linux-device-mapper/"
    >Linux device mapper</a> 
</h2>
 

      
    </header>
     
    <div class="article-meta">
      <a href="/2023/04/15/Linux-device-mapper/" class="article-date">
  <time datetime="2023-04-15T14:19:56.000Z" itemprop="datePublished">2023-04-15</time>
</a> 
  <div class="article-category">
    <a class="article-category-link" href="/categories/GBase8s/">GBase8s</a>
  </div>
   
    </div>
      
    <div class="article-entry" itemprop="articleBody">
       
  <h2 id="1-功能："><a href="#1-功能：" class="headerlink" title="1.功能："></a>1.功能：</h2><p>Linux device mapper 可以将多个物理设备合成为一个逻辑设备，可以做普通的合并，或者实现类似<code>raid0</code>的条带化，还可以用来屏蔽硬盘中的坏道扇区，还可做lvm快照来备份数据库，或者通过零设备文件来模拟非常大的设备，用于测试功能。 <code>Device mapper</code> 是<code>lvm</code> 和 <code>multipating</code> 的底层技术。</p>
<h2 id="2-安装包："><a href="#2-安装包：" class="headerlink" title="2.安装包："></a>2.安装包：</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">device-mapper</span><br><span class="line">device-mapper-multipath</span><br></pre></td></tr></table></figure>

<h2 id="3-工作原理："><a href="#3-工作原理：" class="headerlink" title="3.工作原理："></a>3.工作原理：</h2><p>通过mapping table来创建逻辑设备（物理设备和逻辑设备每个扇区之间的对应关系）。</p>
<p>表内容包括：<br> 逻辑设备的起始扇区：<br> 通常是0 逻辑设备的扇区数量 类型（<code>linear</code>线性，连续组合；<code>striped</code>条带化；<code>error</code>屏蔽坏道；<code>snapshot</code>快照；<code>zero</code>零设备）</p>
<h2 id="4-创建逻辑设备映射表："><a href="#4-创建逻辑设备映射表：" class="headerlink" title="4.创建逻辑设备映射表："></a>4.创建逻辑设备映射表：</h2><p>1扇区&#x3D;512字节b</p>
<p>比如10G的磁盘，扇区数为：20000000个扇区.</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">blockdev --getsize /dev/sda6 查看设备扇区数量</span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash"><span class="built_in">echo</span> <span class="string">&quot;0 `blockdev --getsize /dev/sda6` linear /dev/sda6 0&quot;</span> | dmsetup create mydevice</span> </span><br></pre></td></tr></table></figure>

<p>通过table创建逻辑设备 0表示这个逻辑设备从0扇区开始，有208782个扇区，linear表示连续的,&#x2F;dev&#x2F;sda6 0  表示从&#x2F;dev&#x2F;sda6的第0个扇区开始做逻辑设备。当一个设备用剩余空间做逻辑设备时候，扇区就不是从0开始了。 以下写入开机脚本后重启才会生效</p>
<h2 id="5-linear类型设备-特性："><a href="#5-linear类型设备-特性：" class="headerlink" title="5.linear类型设备 特性："></a>5.linear类型设备 特性：</h2><p>把多个物理分区的扇区连续起来组合成一个逻辑设备。</p>
<p> 0 20000 linear &#x2F;dev&#x2F;sda1 0 </p>
<p>20000 60000 linear &#x2F;dev&#x2F;sdb1 0</p>
<p> 注： 逻辑设备从0到扇区取自sda1从0扇区开始取20000扇区，逻辑设备从20000扇区开始，从sdb1的0扇区开始取60000个扇区，该逻辑设备80000个扇区。 实现命令</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash"><span class="built_in">echo</span> “0 20000 linear /dev/sda1 0\n20000 60000 linear /dev/sdb1 0” | dmsetup create mydevice</span></span><br></pre></td></tr></table></figure>

<h2 id="6-stripe条带化："><a href="#6-stripe条带化：" class="headerlink" title="6.stripe条带化："></a>6.stripe条带化：</h2><p>通过<code>chunksize</code> 轮流写入磁盘 </p>
<p>0 1024 striped 2 256 &#x2F;dev&#x2F;sda1 0 &#x2F;dev&#x2F;sdb1 0 </p>
<p>注： 逻辑设备从0扇区开始到1024扇区，类型为striped ,2个设备, chunksize 256kb 从&#x2F;dev&#x2F;sda1和&#x2F;dev&#x2F;sdb1 的0 扇区开始 各取512扇区(注意扇区数必须是chunksize的倍数) 命令实现</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash"><span class="built_in">echo</span> “0 1024 striped 2 256 /dev/sda1 0 /dev/sdb1 0” | dmsetup create mydevice</span></span><br></pre></td></tr></table></figure>

<h2 id="7-error"><a href="#7-error" class="headerlink" title="7.error:"></a>7.error:</h2><p>通过合成逻辑设备去除错误扇区 0 80 linear &#x2F;dev&#x2F;sda1 0 80 100 error 181 200 linear &#x2F;dev&#x2F;sdb1 0 命令实现</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash"><span class="built_in">echo</span> “0 80 linear /dev/sda1 0\n80 100 error\n181 200 linear /dev/sdb1 0” | dmsetup create mydevice</span></span><br></pre></td></tr></table></figure>

<h2 id="8-snapshot逻辑卷快照-特性："><a href="#8-snapshot逻辑卷快照-特性：" class="headerlink" title="8.snapshot逻辑卷快照 特性："></a>8.snapshot逻辑卷快照 特性：</h2><p>创建快照后，出现3个设备（原设备，快照设备，cow设备） 如果数据没有改变，读取数据从原设备读取，写入变化的数据存储到cow区域中，快照设备保存原设备的数据。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash"><span class="built_in">echo</span> ―0 1000 snapshot /dev/sda1 /dev/vg0/realdev P 16 ‖ | dmsetup create mydevice从0扇区到1000扇区为/dev/sda1创建快照，名字为realdev,P表示下次启动仍然生效，16为chunksize</span></span><br></pre></td></tr></table></figure>

<h2 id="9-zero零设备特性："><a href="#9-zero零设备特性：" class="headerlink" title="9.zero零设备特性："></a>9.zero零设备特性：</h2><p>类似&#x2F;dev&#x2F;zero，但是他是个块设备，不能写东西，一般用来测试用，创建大的文件系统进行测试。比如测试创建10T大小的设备用ext3来格式化</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash"><span class="built_in">export</span> HUGESIZE=$[100 * (2**40)/512] 100T的扇区数量 2**40为2的40次方</span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash"><span class="built_in">echo</span> <span class="string">&quot;0 <span class="variable">$HUGESIZE</span> zero&quot;</span> | dmsetup create zerodev 生成的文件在/dev/mapper/zerodev ext3每个分区最大支持2TB</span></span><br></pre></td></tr></table></figure>

<h2 id="10-多路径功能特性："><a href="#10-多路径功能特性：" class="headerlink" title="10.多路径功能特性："></a>10.多路径功能特性：</h2><p>多路径功能，用来提供线路冗余，监控每条链路，当链路失败时自动切换链路，而且自动恢复运行，防止单点故障。生成的设备名 &#x2F;dev&#x2F;dm-X 类型：<br> 当两路径优先级相等：<br> 负载均衡 当两路径优先级不等: 冗余</p>
<h2 id="11-multipath列出多路径设备"><a href="#11-multipath列出多路径设备" class="headerlink" title="11.multipath列出多路径设备:"></a>11.multipath列出多路径设备:</h2><p>后台需要开启multipathd服务，优先级大小为0-1024 实验步骤：</p>
<p>存储端配置双网卡，配置&#x2F;dev&#x2F;sda6为iscsi设备 服务器端安装device-mapper-multipath包，连接iscsi设备</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">vi /etc/multipath.conf 注释掉 blacklist &#123; devnode <span class="string">&quot;*&quot;</span> 不同厂商的配置是不一样的 &#125; 取消注释 default&#123; udev_dir .. .. path_grouping_policy failover(根据失效域来判断执行策略) &#125;</span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">systemctl <span class="built_in">enable</span> multipathd</span> </span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">systemctl restart multipathd 之后生成的设备位置在/dev/mpath/下,可制作文件系统，挂载</span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">multipath –ll 查询设备状态</span></span><br></pre></td></tr></table></figure>

<h2 id="12-FC存储"><a href="#12-FC存储" class="headerlink" title="12.FC存储:"></a>12.FC存储:</h2><p>存储端建立raid设备，raid建立与HBA卡WWN号的映射关系（连接哪个HBA卡则使用哪块磁盘设备） WWN为HBA卡的授权名称，用来区分一个或一组网络连接，表示网络上的一个连接。</p>
<h2 id="鸣谢："><a href="#鸣谢：" class="headerlink" title="鸣谢："></a>鸣谢：</h2><p>原文出自： <a target="_blank" rel="noopener" href="https://www.cnblogs.com/my-show-time/p/15152982.html">https://www.cnblogs.com/my-show-time/p/15152982.html</a></p>
 
      <!-- reward -->
      
    </div>
    

    <!-- copyright -->
    
    <footer class="article-footer">
       
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/level3/" rel="tag">level3</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/lvm/" rel="tag">lvm</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/multipath/" rel="tag">multipath</a></li></ul>

    </footer>
  </div>

   
   
  
</article>

    
  </article>
  

  
  <nav class="page-nav">
    
    <a class="extend prev" rel="prev" href="/page/5/">上一页</a><a class="page-number" href="/">1</a><span class="space">&hellip;</span><a class="page-number" href="/page/4/">4</a><a class="page-number" href="/page/5/">5</a><span class="page-number current">6</span>
  </nav>
  
</section>
</div>

      <footer class="footer">
  <div class="outer">
    <ul>
      <li>
        Copyrights &copy;
        2023-2024
        <i class="ri-heart-fill heart_icon"></i> Wang Wei
      </li>
    </ul>
    <ul>
      <li>
        
      </li>
    </ul>
    <ul>
      <li>
        
        
        <span>
  <span><i class="ri-user-3-fill"></i>Visitors:<span id="busuanzi_value_site_uv"></span></span>
  <span class="division">|</span>
  <span><i class="ri-eye-fill"></i>Views:<span id="busuanzi_value_page_pv"></span></span>
</span>
        
      </li>
    </ul>
    <ul>
      
    </ul>
    <ul>
      
    </ul>
    <ul>
      <li>
        <!-- cnzz统计 -->
        
      </li>
    </ul>
  </div>
</footer>    
    </main>
    <div class="float_btns">
      <div class="totop" id="totop">
  <i class="ri-arrow-up-line"></i>
</div>

    </div>
    <aside class="sidebar on">
      <button class="navbar-toggle"></button>
<nav class="navbar">
  
  <div class="logo">
    <a href="/"><img src="/images/ww.ico" alt="朝花夕拾"></a>
  </div>
  
  <ul class="nav nav-main">
    
    <li class="nav-item">
      <a class="nav-item-link" href="/">主页</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/archives">归档</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/categories">分类</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags">标签</a>
    </li>
    
  </ul>
</nav>
<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      
      <a class="nav-item-link nav-item-search"  title="Search">
        <i class="ri-search-line"></i>
      </a>
      
      
    </li>
  </ul>
</nav>
<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
    </aside>
    <div id="mask"></div>

<!-- #reward -->
<div id="reward">
  <span class="close"><i class="ri-close-line"></i></span>
  <p class="reward-p"><i class="ri-cup-line"></i>请我喝杯咖啡吧~</p>
  <div class="reward-box">
    
    <div class="reward-item">
      <img class="reward-img" src="/images/alipay.jpg">
      <span class="reward-type">支付宝</span>
    </div>
    
    
    <div class="reward-item">
      <img class="reward-img" src="/images/wechat.jpg">
      <span class="reward-type">微信</span>
    </div>
    
  </div>
</div>
    
<script src="/js/jquery-3.6.0.min.js"></script>
 
<script src="/js/lazyload.min.js"></script>

<!-- Tocbot -->

<script src="https://cdn.staticfile.org/jquery-modal/0.9.2/jquery.modal.min.js"></script>
<link
  rel="stylesheet"
  href="https://cdn.staticfile.org/jquery-modal/0.9.2/jquery.modal.min.css"
/>
<script src="https://cdn.staticfile.org/justifiedGallery/3.8.1/js/jquery.justifiedGallery.min.js"></script>

<script src="/dist/main.js"></script>

<!-- ImageViewer -->

<!-- MathJax -->

<!-- Katex -->

<!-- busuanzi  -->
 
<script src="/js/busuanzi-2.3.pure.min.js"></script>
 
<!-- ClickLove -->

<!-- ClickBoom1 -->

<!-- ClickBoom2 -->

<!-- CodeCopy -->
 
<link rel="stylesheet" href="/css/clipboard.css">
 <script src="https://cdn.staticfile.org/clipboard.js/2.0.10/clipboard.min.js"></script>
<script>
  function wait(callback, seconds) {
    var timelag = null;
    timelag = window.setTimeout(callback, seconds);
  }
  !function (e, t, a) {
    var initCopyCode = function(){
      var copyHtml = '';
      copyHtml += '<button class="btn-copy" data-clipboard-snippet="">';
      copyHtml += '<i class="ri-file-copy-2-line"></i><span>COPY</span>';
      copyHtml += '</button>';
      $(".highlight .code pre").before(copyHtml);
      $(".article pre code").before(copyHtml);
      var clipboard = new ClipboardJS('.btn-copy', {
        target: function(trigger) {
          return trigger.nextElementSibling;
        }
      });
      clipboard.on('success', function(e) {
        let $btn = $(e.trigger);
        $btn.addClass('copied');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-checkbox-circle-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPIED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-checkbox-circle-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
      clipboard.on('error', function(e) {
        e.clearSelection();
        let $btn = $(e.trigger);
        $btn.addClass('copy-failed');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-time-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPY FAILED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-time-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
    }
    initCopyCode();
  }(window, document);
</script>
 
<!-- CanvasBackground -->

<script>
  if (window.mermaid) {
    mermaid.initialize({ theme: "forest" });
  }
</script>


    
    

  </div>
</body>

</html>